home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / amiga / exec_dcc.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  112 lines

  1.  
  2. /*
  3.  *  SYSTEM_DCC.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  *
  9.  *  LoadSeg() and run a dcc program as this process.  ONLY DCC programs may
  10.  *  be run with this call.  The resident list is searched properly.
  11.  *
  12.  *  uses cli_DefaultStack if we are a process and the cli exists, else uses
  13.  *  a 4K stack.   Also modifies cli_CommandName and cli_Module temporarily.
  14.  */
  15.  
  16. #define SysBase_DECLARED
  17.  
  18. #include <exec/types.h>
  19. #include <exec/ports.h>
  20. #include <exec/memory.h>
  21. #include <exec/execbase.h>
  22. #include <libraries/dosextens.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/dos_protos.h>
  25. #include <lib/bcpl.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <lib/misc.h>
  31.  
  32.  
  33. typedef struct CommandLineInterface CLI;
  34.  
  35. extern struct ExecBase    *SysBase;
  36.  
  37. exec_dcc(cmd, args)
  38. char *cmd;
  39. char *args;
  40. {
  41.     long r;
  42.     long seg;
  43.     long unload = 0;
  44.     long stackSize;
  45.     char *stack;
  46.     CLI *cli = NULL;
  47.  
  48.     if ((seg = _SearchResident(cmd)) == NULL) {
  49.     unload = 1;
  50.     if ((seg = LoadSeg(cmd)) == NULL) {
  51.         long lock;
  52.         long oldLock;
  53.         if (lock = _SearchPath(cmd)) {
  54.         oldLock = CurrentDir(lock);
  55.         seg = LoadSeg("");
  56.         UnLock(CurrentDir(oldLock));
  57.         }
  58.     }
  59.     }
  60.     if (seg == NULL) {
  61.     errno = ENOTFND;
  62.     return(-1);
  63.     }
  64.  
  65.     /*
  66.      *    allocate stack, make call, deallocate stuff.  p.s. do not need the -4.
  67.      */
  68.  
  69.     stackSize = 4096;
  70.     if (SysBase->ThisTask->tc_Node.ln_Type != NT_TASK) {
  71.     cli = BTOC(((struct Process *)SysBase->ThisTask)->pr_CLI, CLI);
  72.  
  73.     if (cli)
  74.         stackSize = cli->cli_DefaultStack * 4;
  75.     }
  76.     if (stack = AllocMem(stackSize, MEMF_PUBLIC)) {
  77.     char *cmdName;
  78.     BPTR oldModule;
  79.     BPTR oldName;
  80.  
  81.     if (cli) {
  82.             oldModule = cli->cli_Module;
  83.             oldName   = cli->cli_CommandName;
  84.             cli->cli_Module = seg;
  85.  
  86.         cmdName = malloc(strlen(cmd) + 3);
  87.  
  88.         if (cmdName)
  89.         {
  90.                 strcpy(cmdName + 1, cmd);
  91.                 strcat(cmdName, "\n");
  92.                 cmdName[0] = strlen(cmd);
  93.  
  94.                 cli->cli_CommandName = CTOB(cmdName);
  95.         }
  96.     }
  97.     r = _ExecSeg(seg, args, strlen(args), stack + stackSize - 4);
  98.  
  99.     FreeMem(stack, stackSize);
  100.  
  101.     if (cli) {
  102.         cli->cli_Module = oldModule;
  103.         cli->cli_CommandName = oldName;
  104.         free(cmdName);
  105.     }
  106.     }
  107.     if (unload)
  108.     UnLoadSeg(seg);
  109.     return(r);
  110. }
  111.  
  112.